package com.kangyi.admin.controller;

import com.kangyi.admin.service.NursingHomeAccountService;
import com.kangyi.dto.BulkOperationDTO;
import com.kangyi.dto.EditNursingHomeAccountDTO;
import com.kangyi.dto.NursingHomeAccountPageQueryDTO;
import com.kangyi.result.NursingHomeAccountPageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.DetailNursingHomeAccountVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/nursingHomeAccount")
@Slf4j
@Api(tags = "超级管理员-养老院账号管理相关接口")
public class NursingHomeAccountController {

    @Autowired
    private NursingHomeAccountService nursingHomeAccountService;

    @GetMapping("/page")
    @ApiOperation("养老院账号分页查询")
    public Result<NursingHomeAccountPageResult> pageQuery(NursingHomeAccountPageQueryDTO nursingHomeAccountPageQueryDTO){
        log.info("养老院账号分页查询:{}", nursingHomeAccountPageQueryDTO);
        try {
            NursingHomeAccountPageResult nursingHomeAccountPageResult = nursingHomeAccountService.pageQuery(nursingHomeAccountPageQueryDTO);
            return Result.success(nursingHomeAccountPageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @PutMapping("/operation")
    @ApiOperation("批量操作启用/禁用")
    @CacheEvict(cacheNames = "nursingHomeAccount_", allEntries = true)
    public Result bulkOperation(@RequestBody BulkOperationDTO bulkOperationDTO){
        log.info("批量操作启用/禁用:{}",bulkOperationDTO);
        try {
            nursingHomeAccountService.bulkOperation(bulkOperationDTO);
            return Result.success("操作成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除账号")
    @CacheEvict(cacheNames = "nursingHomeAccount_", allEntries = true)
    public Result deleteManageAccount(@RequestParam List<Integer> ids){
        log.info("批量删除账号:{}",ids);
        try {
            nursingHomeAccountService.deleteManageAccount(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("查询账号详情")
    @CacheEvict(cacheNames = "nursingHomeAccount_", allEntries = true)
    public Result<DetailNursingHomeAccountVO> detailAccount(@PathVariable Integer id){
        log.info("查询账号详情:{}",id);
        try {
            DetailNursingHomeAccountVO detailNursingHomeAccountVO = nursingHomeAccountService.detailAccount(id);
            return Result.success(detailNursingHomeAccountVO,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/edit")
    @ApiOperation("编辑养老院账号")
    @CacheEvict(cacheNames = "nursingHomeAccount_", allEntries = true)
    public Result editAccount(@RequestBody EditNursingHomeAccountDTO editNursingHomeAccountDTO){
        log.info("编辑养老院账号:{}",editNursingHomeAccountDTO);
        try {
            nursingHomeAccountService.editAccount(editNursingHomeAccountDTO);
            return Result.success("操作成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
